package com.bridgeintelligent.tag.customer.service;

import com.bridgeintelligent.tag.constants.TagConstants;
import com.bridgeintelligent.tag.customer.entity.CustomersCodes;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.exception.CustomersException;
import com.bridgeintelligent.tag.customer.mapper.CustomersMapper;
import com.bridgeintelligent.tag.customer.model.CustomersStopModel;
import com.bridgeintelligent.tag.customer.model.CustomersStopQueryModel;
import com.bridgeintelligent.tag.customer.pojo.Customers;
import com.bridgeintelligent.tag.customer.pojo.CustomersApprovalRecord;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wayneleo.quickstart.framework.ExceptionCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @description
 * @author: grp 2021-03-03 21:33
 **/
@Service
@AllArgsConstructor
@Slf4j
public class CustomersStopServiceImpl implements ICustomersStopService{

    private final CustomersMapper customersMapper;
    private final CustomersService customersService;
    private SqlSessionTemplate sqlSessionTemplate;
    private ICustomerApprovalService customerApprovalService;

    @Override
    public CustomersPage<Customers> list(CustomersStopQueryModel model) {
        //获取当前用户的id
        Page page = PageHelper.startPage(model.getPageNumber(), model.getPageSize());
        List<Customers> list = customersMapper.findByDimId(model);
        if (list != null && !list.isEmpty()) {
            for (Customers customers : list) {
                if (CustomersCodes.STATUS_01.equals(customers.getStatus())) {
                    customers.setStatusName("有效");
                } else if (CustomersCodes.STATUS_02.equals(customers.getStatus())) {
                    customers.setStatusName("失效");
                }
                if ("1".equals(customers.getApprovalStatus())) {
                    customers.setApprovalStatusName("未提交");
                } else if ("2".equals(customers.getApprovalStatus())) {
                    customers.setApprovalStatusName("待审核");
                } else if ("3".equals(customers.getApprovalStatus())) {
                    customers.setApprovalStatusName("驳回");
                } else if ("4".equals(customers.getApprovalStatus())) {
                    customers.setApprovalStatusName("已通过");
                }
            }
        }
        CustomersPage<Customers> customersPage = new CustomersPage<>();
        customersPage.setContent(list);
        customersPage.setTotalPages(page.getPages());
        customersPage.setTotalElements(page.getTotal());
        customersPage.setSize(page.getPageSize());
        customersPage.setRequestPager(model.getPageNumber());
        return customersPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomersStopModel model) {
        if(model.isAll()){
            if(StringUtils.isBlank(model.getDimId())){
                throw new CustomersException(new ExceptionCode(999, "批量停用客群失败！"));
            }
            List<Customers> deploys = customersMapper.findDeploysByDimId(model.getDimId());
            deploys.forEach(customers->customerApprovalService.deleteCommit(customers.getCustomersId()));
            List<Customers> unDeploys = customersMapper.findUnDeploysByDimId(model.getDimId());
            unDeploys.forEach(customers->customersService.delCustomers(customers.getCustomersId()));
            return;
        }
        if(CollectionUtils.isNotEmpty(model.getIds())){
            model.getIds().forEach(id->{
                Customers customers = customersMapper.findOneById(id);
                if(StringUtils.equals(customers.getApprovalStatus(), "2")||
                        StringUtils.equals(customers.getApprovalStatus(), "3")||
                        StringUtils.equals(customers.getApprovalStatus(), "4")){
                    customerApprovalService.deleteCommit(id);
                }else{
                    customersService.delCustomers(id);
                }
            });
        }
    }
}
